package light.head.controller.oa;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.oa.ConsumingDAO;
import light.head.dao.oa.ConsumingDetailDAO;
import light.head.dao.oa.ConsumingItemDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.Consuming;
import light.head.model.ConsumingDetail;
import light.head.model.ConsumingItem;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/***
 * 物品领用
 * @author yy
 *
 */
@Controller
@RequestMapping(Url.C_CONSUMING)
public class ConsumingController extends SupporterController {
	
	private ConsumingDAO  conDAO;
	private ConsumingDetailDAO detailDAO;
	private ConsumingItemDAO itemDAO;
	private OrganDAO organDAO;
	
	@RequestMapping(Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse reponse) throws Exception {
		return new ModelAndView(Url.CONSUMING_INIT);
	}
	
	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse reponse) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.DELETED, Parm.CN,Parm.NAME, Parm.TIME1, Parm.TIME2, Parm.NAME);
		Integer oc = organDAO.getRoot(cu_organCode(request)).getCode();
		if (RESULT.SUCCESS == conDAO.findByMap(organDAO.getSonCodeList(oc), pb, map)) {
			List<Object> conCodes = new ArrayList<Object>();
			for (Object o : pb.getResultList()) {
				Consuming con = (Consuming) o;
				conCodes.add(con.getCode());
			}
			// 将对应的明细放到对应的领用信息里
			List<ConsumingItem> riList = itemDAO.findByConCodes(conCodes);
			if (null != riList && riList.size() > 0) {
				for (Object o : pb.getResultList()) {
					Consuming con = (Consuming) o;
					con.setItemList(new ArrayList<ConsumingItem>());
					for (ConsumingItem ci : riList) {
						if (con.getCode().intValue() == ci.getCoc().intValue()) {
							con.getItemList().add(ci);
						}
					}
				}
			}
		}
		request.setAttribute(Parm.PAGE_BEAN, pb);
		return new ModelAndView(Url.CONSUMING_LIST);
	}
	
	/**
	 * 显示标签
	 */
	@RequestMapping(Url.SHOW_TAG)
	public ModelAndView showTag(Integer code,HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (code != null) {
			Consuming con = conDAO.get(code);
			if (con != null) {
				// 获得所有明细
				List<ConsumingItem> itemList = itemDAO.findByConCode(code);
				// 获取所有详情
				List<ConsumingDetail> detailList = detailDAO.findByRCode(code);
				// 循环将对应的详情放到对应的明细里面
				for (ConsumingItem item : itemList) {
						item.setDetailList(new ArrayList<ConsumingDetail>());
					
					for (ConsumingDetail detail : detailList) {
						// 判断明细的 编号 和 详情的编号相等的就放到对应的明细里面
						if (item.getCode().intValue() == detail.getCic().intValue()) {
							item.getDetailList().add(detail);
						}
					}
				}
				con.setItemList(itemList);
				request.setAttribute(Parm.INFO, con);
			}
		}
		return new ModelAndView(Url.CONSUMING_SHOW_TAG);
	}
	
	@Autowired
	public void setConDAO(ConsumingDAO conDAO) {
		this.conDAO = conDAO;
	}
	@Autowired
	public void setItemDAO(ConsumingItemDAO itemDAO) {
		this.itemDAO = itemDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
	@Autowired
	public void setDetailDAO(ConsumingDetailDAO detailDAO) {
		this.detailDAO = detailDAO;
	}
	
	

}